package com.bdqn.controller;



import java.io.UnsupportedEncodingException;
import java.net.URLEncoder;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;

import com.bdqn.dto.ContactLists;
import com.bdqn.dto.MsgDto;
import com.bdqn.model.ContactList;
import com.bdqn.service.ContactListService;






@Controller
@RequestMapping("/contact")
public class ContactListController {

	@Autowired
	private ContactListService contactListService;
	
	@RequestMapping("/delete/{id}")
	public String delete(@PathVariable("id") Integer id,@RequestParam("cid")Integer cid) {
		
		System.out.println(cid);
		int num = contactListService.delete(id);
		if(num==1) {
		return "redirect:/custom/toAlter/"+cid;
		}
		return null;
		
	}
	@RequestMapping("/alter/{agent}")
	public String alter(ContactLists list,@PathVariable("agent") String agent) throws UnsupportedEncodingException {
		
		List<ContactList> lists = list.getLists();
		
		ContactList cl =new ContactList();
		System.out.println(agent);
		int num = 0;
		int updateNum=0;
		int insertNum=0;
		
		for (int i = 0; i < lists.size(); i++) {
			
			cl=lists.get(i);
			num = contactListService.select(cl.getId());
			
			if(num==1) {
				updateNum =contactListService.update(cl);
			}else {
				insertNum = contactListService.insert(cl);
			}
			
		}
		int count = updateNum+insertNum;
		
		if(count>0) {
			
			MsgDto msg = new MsgDto();
			
			msg.setMessage("保存成功");
			
			return "redirect:/custom/likeSearch?companyName=&agent="+URLEncoder.encode(agent,"UTF-8")+"&message="+URLEncoder.encode(msg.getMessage(),"UTF-8");
		}
		return null;
		
		
		
		
	}
}
